package com.kj.tms.common.utils;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author:zhuyongjie
 * @Description:String工具类
 * @Date:Create in  2017/10/24 11:08
 */
public class StringUtil {

    /**
     * 转换为字符串
     *
     * @param obj
     * @return
     */
    public static String toString(Object obj) {
        return obj != null ? obj.toString() : "";
    }

    /**
     * 判断是否为空或null
     *
     * @param obj
     * @return
     */
    public static boolean isEmptyOrNull(Object obj) {
        return obj == null || "".equals(obj.toString().trim());
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * 转换为int类型
     *
     * @param obj
     * @return
     */
    public static int toInt(Object obj) {
        String str = obj != null ? obj.toString().trim() : "";
        return "".equals(str) ? 0 : Integer.parseInt(str);
    }


    /**
     * 转换为double类型
     *
     * @param obj
     * @return
     */
    public static double toDouble(Object obj) {
        String str = obj != null ? obj.toString().trim() : "";
        return "".equals(str) ? 0.0D : Double.parseDouble(str);
    }

    /**
     * @param obj
     * @return
     * @throws
     * @method decodeToUtf
     * @description [将通过encodeURL编码的值解码]
     * @author [zhuyongjie]
     * @version 1.0
     * @date 2017/10/31 15:55
     */
    public static String decodeToUtf(Object obj) {
        String result = obj != null ? obj.toString() : "";
        try {
            result = URLDecoder.decode(result, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @return
     * @throws
     * @method decodeTOUtf
     * @description [将通过encodeURL编码的值解码, 为空时赋予默认值]
     * @author [zhuyongjie]
     * @version 1.0
     * @paras
     * @date 2017/10/31 15:59
     */
    public static String decodeTOUtf(Object obj, String str) {
        String result = obj != null ? obj.toString() : str;
        try {
            result = URLDecoder.decode(result, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * @param object
     * @return
     * @throws
     * @method toBigDecimal
     * @description [转换为BigDecimal]
     * @author [zhuyongjie]
     * @version 1.0
     * @paras
     * @date 2017/11/1 10:28
     */
    public static BigDecimal toBigDecimal(Object object) {
        try {
            if (object == null) {
                return new BigDecimal(0.00);
            }
            BigDecimal bigDecimal = new BigDecimal(toString(object));
            return bigDecimal;
        } catch (Exception e) {
            return new BigDecimal(0.00);
        }
    }

    public static boolean isNull(Object obj) {
        boolean isNullFlag = true;
        if (obj != null) {
            if (obj instanceof List) {
                isNullFlag = isNull((List) obj);
            } else if (obj instanceof Set) {
                isNullFlag = isNull((Set) obj);
            } else if (obj instanceof Object[]) {
                isNullFlag = isNull((Object[]) ((Object[]) obj));
            } else if (obj instanceof Map) {
                isNullFlag = isNull((Map) obj);
            } else if (obj instanceof String) {
                isNullFlag = isNull((String) obj);
            } else {
                isNullFlag = false;
            }
        }

        return isNullFlag;
    }

    public static boolean isNull(List<?> list) {
        return list == null || list.size() == 0;
    }

    public static boolean isNull(Set<?> set) {
        return set == null || set.size() == 0;
    }

    public static boolean isNull(Object[] objects) {
        return objects == null || objects.length == 0;
    }

    public static boolean isNull(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    public static boolean isNull(String str) {
        return str == null || "".equals(str.trim()) || "null".equalsIgnoreCase(str.trim());
    }

    public static boolean isNotNull(Object obj) {
        return !isNull(obj);
    }

    public static boolean isBlank(CharSequence cs) {
        int strLen;
        if (cs != null && (strLen = cs.length()) != 0) {
            for(int i = 0; i < strLen; ++i) {
                if (!Character.isWhitespace(cs.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }
    /**
     * @Method      setParameter
     * @Param       [str, map]
     * @Return      java.lang.String
     * @Exception
     * @Description [将字符串中的([])去掉并把参数转换为对应的值]
     * @Author      wangmeng
     * @Version     1.0
     * @Date        2018/7/16 16:32
     */
    public static String setParameter(String str,Map<String,String> map) {
        String returnStr = str.replace("([", "").replace("])", "");
        List<String> results = new ArrayList<String>();
        Pattern p = Pattern.compile("\\#(.*?)\\#");
        Matcher m = p.matcher(returnStr);
        while (m.find()) {
            results.add(m.group(1));
        }
        for (int i = 0; i < results.size(); i++) {
            returnStr = returnStr.replace("#"+results.get(i).toString()+"#", "'"+map.get(results.get(i).toString())+"'");
        }
        return returnStr;
    }

}
